package org.six.background.service.impl;

import org.six.background.mapper.OutshopMapper;
import org.six.background.mapper.ShopMapper;
import org.six.background.mapper.UserMapper;
import org.six.background.pojo.*;
import org.six.background.service.OutshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OutshopServiceImpl implements OutshopService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private ShopMapper shopMapper;
    @Autowired(required = false)
    private OutshopMapper outshopMapper;

    @Override
    public List<Outshop> outshopshow() {
        System.out.println("值");
        System.out.println(outshopMapper.outshopshow().toString());
        return outshopMapper.outshopshow();
    }

    @Override
    @Transactional(timeout = 1000)
    public String outshopAdd(String usname,int outidcard,int usphone,String shopname,int outnum) {
        System.out.println("outttt");
        System.out.println(usname);
        System.out.println(outidcard);
        System.out.println(usphone);
        System.out.println(shopname);
        System.out.println(outnum);
      //
        ShopExample shopExample=new ShopExample();
        ShopExample.Criteria sc=shopExample.createCriteria();
        sc.andShopnameEqualTo(shopname);
        List<Shop> shops=shopMapper.selectByExample(shopExample);
        int shopid=shops.get(0).getShopid();
        int shopnumout=shops.get(0).getShopnum();



        //
        System.out.println("shopid="+shopid);
        System.out.println("shopnumout="+shopnumout);

        if (outnum>shopnumout){
            return "商品不足";
        }
        else {
            int shopnum=shops.get(0).getShopnum()-outnum;
            Shop shop= shopMapper.selectByPrimaryKey(shopid);
            shop.setShopnum(shopnum);
            shopMapper.updateByPrimaryKeySelective(shop);
            System.out.println("更改的数量："+shop.getShopnum());
            Outshop outshop=new Outshop();
            User user=new User();
            UserExample userExample=new UserExample();
            UserExample.Criteria uc=userExample.createCriteria();
            uc.andUsnameEqualTo(usname);
            List<User> users=userMapper.selectByExample(userExample);
            int usid=users.get(0).getUsid();

            Date date=new Date();
            outshop.setOutaddtime(date);
            outshop.setOutnum(outnum);
            outshop.setOutidcard(outidcard);
            outshop.setUsid(usid);
            outshop.setOutusname(usname);
            outshop.setShopid(shopid);
            System.out.println(outshop);
            int i=outshopMapper.insertSelective(outshop);
            if (i==1){
                return "ok";
            }
            return "no";
        }




    }
    @Override
    public Map outshopAll(int page, int pagesize) {
        int count =outshopMapper.countByExample(null);
        double pageMax=Math.ceil(count/pagesize)+1;
        Map pages=new HashMap();
        pages.put("page",page);
        pages.put("pagesize",pagesize);
        pages.put("count",count);
        pages.put("pagemax",pageMax);
        page=page>(int)(pageMax)?(int)(pageMax):page;

        Map map1=new HashMap();
        map1.put("index",(page-1)*pagesize);
        map1.put("pagesize",pagesize);

        List<Outshop> outshops = outshopMapper.outshopAll(map1);

        Map map=new HashMap();
        map.put("outshops",outshops);
        map.put("pages",pages);
        return map;
    }
}
